package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataFactory {
	private static SimpleDateFormat defaultFmt = new SimpleDateFormat("yyyy-MM-dd");
	public static void log(String content) {
		System.out.println(content);
		String logFileName = "log/log_" + getDate(null);
		File file = new File(logFileName);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdir();
		}
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new FileOutputStream(logFileName, true));
			pw.write(content + System.getProperty("line.separator"));
			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();
			}
		}
	}
	public static String getDate(String fmt) {
		if (fmt == null || fmt.equals("")) {
			return defaultFmt.format(new Date());
		}
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(new Date());
	}
}
